#include "multistepcontrol.h"

MultiStepControl::MultiStepControl(QWidget *parent) : QWidget(parent)
{

}

MultiStepControl::MultiStepControl(const QString &Name, const QJsonObject &functionDetails, QWidget *parent)
    : QWidget(parent)
{
    QGroupBox *groupBox = new QGroupBox("多步控制", this);
    groupBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    createMultiStepControl(groupBox, functionDetails);

    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(groupBox);
    layout->setContentsMargins(0, 0, 0, 0);
    qDebug() << "测试是否进入多步控制";

    setLayout(layout);
}

void MultiStepControl::createMultiStepControl(QGroupBox *groupBox, const QJsonObject &functionDetails)
{
    qDebug() << "functionDetails :" << functionDetails;
    QVBoxLayout *mainLayout = new QVBoxLayout();
    groupBox->setLayout(mainLayout);
    QJsonObject Details = functionDetails["Parameters"].toObject();

    // 创建用于放置按钮的窗口
    QWidget *PushButtonWidget = new QWidget(groupBox);
    QGridLayout *PushButtonLayout = new QGridLayout(PushButtonWidget);
    PushButtonWidget->setLayout(PushButtonLayout);
    PushButtonWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    QJsonArray pushbuttonArray = Details["pushbutton"].toArray();
    qDebug() << "Pushbutton Array:" << pushbuttonArray;

    // 遍历 pushbuttonArray，为每个按钮设置合适的行列位置
    int row = 0;
    int col = 0;
    for (int i = 0; i < pushbuttonArray.size(); ++i) {
        QString buttonText = pushbuttonArray[i].toString();
        QPushButton *button = new QPushButton(buttonText, PushButtonWidget);
        button->setMaximumSize(200, 60);

        // 将按钮添加到布局中，每行3个按钮
        PushButtonLayout->addWidget(button, row, col);

        // 计算下一个按钮的行列位置
        col++;
        if (col == 3) {
            col = 0;  // 每3个按钮换行
            row++;     // 进入下一行
        }

        connect(button, &QPushButton::clicked, this, &MultiStepControl::onButtonClicked);
    }

    mainLayout->addWidget(PushButtonWidget);
}

void MultiStepControl::onButtonClicked()
{
    QPushButton *button = qobject_cast<QPushButton*>(sender());
    if (!button)
        return;

    QString buttonText = button->text();
    qDebug() << "Button clicked:" << buttonText;

    // 根据按钮的文本执行不同的逻辑
    if (buttonText == "moveCR1") {
       qDebug() << "moveCR1 logic executed";
       // 添加 moveCR1 的逻辑代码
    } else if (buttonText == "moveR1R2") {
       qDebug() << "moveR1R2 logic executed";
       // 添加 moveR1R2 的逻辑代码
    } else if (buttonText == "moveCL1") {
       qDebug() << "moveCL1 logic executed";
       // 添加 moveCL1 的逻辑代码
    } else if (buttonText == "moveL1L2") {
       qDebug() << "moveL1L2 logic executed";
       // 添加 moveL1L2 的逻辑代码
    } else if (buttonText == "moveR1C") {
       qDebug() << "moveR1C logic executed";
       // 添加 moveR1C 的逻辑代码
    } else if (buttonText == "moveR2R1") {
       qDebug() << "moveR2R1 logic executed";
       // 添加 moveR2R1 的逻辑代码
    } else if (buttonText == "moveL1C") {
       qDebug() << "moveL1C logic executed";
       // 添加 moveL1C 的逻辑代码
    } else if (buttonText == "moveL2L1") {
       qDebug() << "moveL2L1 logic executed";
       // 添加 moveL2L1 的逻辑代码
    } else if (buttonText == "stopMultiStep") {
       qDebug() << "stopMultiStep logic executed";
       // 添加 stopMultiStep 的逻辑代码
    } else {
       qDebug() << "Unknown button clicked:" << buttonText;
    }
}
